package game.games.graphictest;

import game.GraphicUtil;
import game.IRenderer;
import game.cellmap.AbstractCellMap;
import game.coord.MapGenerator;
import game.games.carcassonne.IterablePoint;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

public abstract class AbstractShape extends AbstractCellMap<Color> implements IRenderer
{
    private List<Point> exclusivePoints;
    private Point      start;
    
    protected AbstractShape(int width, int height)
    {
        super(false);
        exclusivePoints = new ArrayList<Point>();
        start = new Point(0, 0);
        setMapSize(new Dimension(width, height));
        setShape();
    }
    
    protected abstract void setShape();
    
    protected final void setPoints(Point start, Point end, Color color)
    {
        for (int x = start.x; x <= end.x; x++)
        {
            for (int y = start.y; y <= end.y; y++)
            {
                getCells().setValue(new Point(x, y), color);
            }
        }
    }
    
    protected final void addExclusivePoint(int x, int y)
    {
        exclusivePoints.add(new Point(x, y));
    }
    
    protected final void addExclusivePoints(Point start, Point end)
    {
        for (int x = start.x; x <= end.x; x++)
        {
            for (int y = start.y; y <= end.y; y++)
            {
                addExclusivePoint(x, y);
            }
        }
    }
    
    public final void setStart(Point start)
    {
        this.start = start;
    }
    
    protected void switchColors(Color from, Color to)
    {
        for (IterablePoint<Color> i = getCells().getIterator();i.hasNext();)
        {
            Point position = i.getCurrentPoint();
            Color color = i.next();
            if (color == from && exclusivePoints.contains(position) == false)
            {
                getCells().setValue(position, to);
            }
        }
    }
    
    @Override
    public void render(Graphics g)
    {
        for (IterablePoint<Color> i = getCells().getIterator();i.hasNext();)
        {
            Point position = i.getCurrentPoint();
            Color color = i.next();
            if (color != null)
            {
                GraphicUtil.fillRect(g, new Point(start.x + (position.x * getCellSize()), start.y + (position.y * getCellSize())), 
                                        new Dimension(getCellSize(), getCellSize()), color);
            }
        }
    }

    @Override
    public void sync()
    {
        setCells(MapGenerator.generateList(getMapSize(), Color.white));
    }
}
